package h_udp;

import java.io.IOException;
import java.net.*;

/**
 * @author Cheng-02
 * @version V1.0
 * @Package h_udp
 * @date 2024/4/14 下午3:08
 */
public class send {
    public static void main(String[] args) {

//        1.创建DatagramSocket对象
//        a.空参：端口号随机
//        b.有参：端口号固定

        try {
            DatagramSocket socket = new DatagramSocket();
            byte[] bytes = "nihoa".getBytes();
            InetAddress address = InetAddress.getLocalHost();
            int port = 8888;
            DatagramPacket socket1 = new DatagramPacket(bytes, bytes.length, address, port);
            socket.send(socket1);
            socket.close();


        } catch (SocketException e) {
            throw new RuntimeException(e);
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
//        2，创建DatagramPacket对象，将数据进行打包
//        a.打包要发送的数据->byte[]
//        b.指定接收端的ip
//        c.指定接收端的端口号
//
//        3.发送数据
//        4.释放资源
    }
}
